# 2024/4/17 下午3:01 HFL
import json
import os

import requests

from Utils.log import LoggerUtils


def get_uuid():
    # 获取uuid
    url = 'http://kdtx-test.itheima.net' + '/api/captchaImage'
    resp = requests.get(url=url)
    uuid = resp.json()['uuid']
    return uuid


def get_token():
    url = 'http://kdtx-test.itheima.net' + '/api/login'
    login_json = {"username": "admin",
                  "password": "HM_2023_test",
                  "code": "2",
                  "uuid": get_uuid()}
    resp_login = requests.post(url=url, json=login_json)
    token = resp_login.json()['token']
    return token


def common_assert(response, exp_status, exp_code, exp_msg):
    assert exp_status == response.status_code
    assert exp_code == response.json()['code']
    assert exp_msg in response.json()['msg']


def common_assert1(response, exp_status, exp_code, exp_msg):
    assert exp_status == response.status_code
    assert exp_code == response.json()['code']
    assert exp_msg in response.json()['message']


# 定义变量，用来管理项目的路径
# BASE_PATH = os.path.dirname(__file__)


# 读取json文件的函数
# def read_json(filename, apiname):
#     file_path = BASE_PATH + '../../datas/{}.json'.format(filename)
#     # 读取文件使用with open 方法进行操作，json.load 是将json数据进行转换为字典
#     with open(file_path, encoding='utf-8') as f:
#         data = json.load(f)
#         print(data)
#     # 获取指定接口的值
#     data_list_dict = data.get(apiname)
#     print(data_list_dict)
#     # 将列表嵌套字典的数据转换为，列表嵌套元组的数据
#     data_list = list()
#     # 将获取到的字典数据进行遍历循环
#     for i in data_list_dict:
#         # 将数据转换为元组，再将数据放入到列表中
#         data_list.append(tuple(i.values()))
#     print(f'列表嵌套元组后的数据：{data_list}')
#     # 将数据返回出去
#     return data_list
# 获取当前的根路径
base_path = os.path.dirname(__file__)

def read_json(filename,apiname):
    """
    读取json文件，并将转换为列表嵌套元组的数据形式
    :param filename:
    :param apiname:
    :return:
    """
    # 获取到的json文件位置
    path = base_path +f"../../datas/{filename}.json"
    # json文件进行读取
    with open(path,encoding='utf-8') as f :
        data = json.load(f)
    # 取到想要的接口数据
    data_dict = data.get(apiname)
    # 创建一个空列表
    data_list = list()
    # 遍历字典
    for i in data_dict:
        # 将取到的数据放进data_list列表里面
        data_list.append(tuple(i.values()))
    # 将数据返回出去
    return data_list





if __name__ == '__main__':
    print(read_json("login_data", "login"))
